package com.sun.config;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.Executor;

/**
 * @Description: 配置并行 任务调度的配置类即可
 * @Author hugh.sun
 * @Date 2021/11/30 13:51
 */
@Configuration
public class ScheduleConfig implements SchedulingConfigurer, AsyncConfigurer {
    /**
     * 线程池数量
     *
     */
    private  int corePollsize = 5;
    /*将taskScheduler 注入到bean*/
    @Bean
    public ThreadPoolTaskScheduler taskScheduler(){
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.initialize();
        taskScheduler.setPoolSize(corePollsize);
        return taskScheduler;
    }


    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        /*设置线程池*/
        taskRegistrar.setTaskScheduler(taskScheduler());

    }
    @Override
    public Executor getAsyncExecutor() {
        return taskScheduler();
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return null;
    }
}
